<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col>
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="80px" v-show="showSearch" >
          <el-form-item label="消息标题" prop="messageTitle">
            <el-input v-model="queryParams.messageTitle" placeholder="请输入消息标题" size="small" clearable style="width: 150px"/>
          </el-form-item>
          <el-form-item label="消息内容" prop="messageContent">
            <el-input v-model="queryParams.messageContent" placeholder="请输入消息内容" size="small" clearable style="width: 150px"/>
          </el-form-item>

          <el-form-item label="消息类型" prop="messageTypeCode">
            <el-select @change="handleQuery" v-model="queryParams.messageTypeCode" size="small" clearable style="width: 150px" placeholder="请选择">
              <el-option key="" label="全部" value=""/>
              <el-option
                v-for="item in messageTypeCodeList"
                :key="item.dictCode"
                :label="item.dictName"
                :value="item.dictCode"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="处理状态" prop="handleStatusCode">
            <el-select @change="handleQuery" v-model="queryParams.handleStatusCode" size="small" clearable style="width: 150px" placeholder="请选择">
              <el-option key="" label="全部" value=""/>
              <el-option
                v-for="item in handleStatusCodeList"
                :key="item.dictCode"
                :label="item.dictName"
                :value="item.dictCode"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="消息时间">
            <el-date-picker @change="handleQuery" v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary"  size="mini" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery('queryForm')">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" :disabled="buttonDisabled(total)" icon="el-icon-plus" size="mini" v-hasPermi="['updateSystemMessage']"  @click="updateSystemMessageHandle()">全部处理</el-button>
            </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="querySystemMessagePageResList" :columns="columns"></right-toolbar>
        </el-row>
        <el-table :stripe="true" v-loading="loading" :data="dataList" style="width: 100%" @sort-change='tableSortChange'>
          <!--<el-table-column label="消息标题" align="left" prop="messageTitle"  width="250"/>-->
          <el-table-column label="消息类型" align="left" prop="messageTypeName" width="150" >
            <template slot-scope="scope">
              <span :style="'color:'+scope.row.messageTypeColor">{{scope.row.messageTypeName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="处理状态" align="left" prop="handleStatusName" width="150">
            <template slot-scope="scope">
              <span :style="'color:'+scope.row.handleStatusColor">{{scope.row.handleStatusName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="消息内容" align="left" prop="messageContent" :show-overflow-tooltip="true"/>

          <el-table-column label="消息时间" align="center" prop="createTime" width="200" />

          <el-table-column label="处理时间" align="center" prop="handleTime" width="200" />

          <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" v-if="scope.row.handleStatusCode!=='PROCESSED'"  type="text" v-hasPermi="['updateSystemMessage']"  @click="updateSystemMessageHandleByMessageId(scope.row)">处理</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total>0"  :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.rows" @pagination="querySystemMessagePageResList"/>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import {querySystemMessagePageResList, updateAllSystemMessageHandleStatus, updateSystemMessageHandleStatusByMessageId} from '@/api/system/systemMessage'
  import { getDicListByDictCodeList } from '@/api/common/dict'
  //默认查询参数
  let defaultQueryParams = {
    handleStatusCode: "",
    messageTitle: undefined,
    startTime: undefined,
    endTime: undefined,
    messageContent: undefined,
    messageTypeCode: "",
    page: 1,
    rows: 10
  }
  export default {
    name: 'systemMessageAdmin',
    data() {
      return {
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 显示搜索条件
        showSearch: true,
        //操作日志
        dataList: [],
        // 日期范围
        dateRange: [],
        //处理状态
        handleStatusCodeList: [],
        //消息类型
        messageTypeCodeList: [],
        // 查询参数
        queryParams: { ...defaultQueryParams },
        //列信息
        columns: []
      }
    },
    created() {
      this.querySystemMessagePageResList()
      this.initDict()
    },
    methods: {
      //排序
      tableSortChange(column) {
        //格式化排序参数，构建中
        this.orderByFormat(this.queryParams,column);
        //执行查询方法
        this.querySystemMessagePageResList()
      },
      /** 查询日志列表 */
      querySystemMessagePageResList() {
        this.loading = true
        querySystemMessagePageResList(this.addDateRange(this.queryParams, this.dateRange)).then(res => {
            this.dataList = res.data.records
            this.total = res.data.total
          }
        ).finally(() => {
          //关闭加载特效
          this.loading = false
        })
      },
      /** 处理全部已读 */
      updateSystemMessageHandle() {
        let that = this
        this.$confirm('是否确认全部标记已处理?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          updateAllSystemMessageHandleStatus().then(response => {
            that.$modal.msgSuccess(response.message)
            that.querySystemMessagePageResList();
          });
        }).catch(function (e) {
          console.log(e);
        });
      },
      /** 根据主键id处理 */
      updateSystemMessageHandleByMessageId(row) {
        updateSystemMessageHandleStatusByMessageId({ messageId: row.messageId }).then(response => {
          this.$modal.msgSuccess(response.message)
          this.querySystemMessagePageResList();
        });
      },
      /** 重置按钮操作 */
      resetQuery(fromId) {
        this.dateRange = []
        this.resetForm(fromId)
        this.handleQuery()
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1
        this.querySystemMessagePageResList()
      },
      //获取数据字典
      initDict(){
        let dictArr = ["HANDLING_STATUS","SYSTEM_MESSAGE_TYPE"];
        getDicListByDictCodeList(dictArr).then(res => {
          this.handleStatusCodeList = JSON.parse(JSON.stringify(res.data['HANDLING_STATUS']));
          this.messageTypeCodeList = JSON.parse(JSON.stringify(res.data['SYSTEM_MESSAGE_TYPE']));
        });
      },
    }
  }
</script>
